package com.gexiaobao.pigeon.ui.fragment.mine.tools.line

import android.os.Bundle
import android.text.TextUtils
import androidx.fragment.app.setFragmentResultListener
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.isBOSMapEmpty
import com.gexiaobao.pigeon.app.util.KvUtils
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.FragmentMatchLineQueryBinding
import com.gexiaobao.pigeon.permissions.PermissionInterceptor
import com.gexiaobao.pigeon.viewmodel.MatchLineQueryViewModel
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 15:24
 * @Describe    : 赛线查询
 */
class FragmentMatchLineQuery : BaseFragment<MatchLineQueryViewModel, FragmentMatchLineQueryBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose("天气风向") {
            nav().navigateUp()
        }

        val mStartLon = KvUtils.decodeString(Constant.START_LON)
        val mStartLat = KvUtils.decodeString(Constant.START_LAT)
        if (mStartLon.isNotEmpty() && mStartLat.isNotEmpty()) {
            setStartLonLatData(mStartLon, mStartLat)
        }
        val mEndLon = KvUtils.decodeString(Constant.END_LON)
        val mEndLat = KvUtils.decodeString(Constant.END_LAT)
        if (mEndLon.isNotEmpty() && mEndLat.isNotEmpty()) {
            setEndLonLatData(mEndLon, mEndLat)
        }

        setFragmentResultListener("requestMap") { _, bundle ->
            val result = bundle.getBoolean("type")
            val lon = bundle.getString("relon")
            val lat = bundle.getString("relat")
            if (result) {
                setStartLonLatData(lon, lat)
                lon?.let { KvUtils.encode(Constant.START_LON, it) }
                lat?.let { KvUtils.encode(Constant.START_LAT, it) }
            } else {
                setEndLonLatData(lon, lat)
                lon?.let { KvUtils.encode(Constant.END_LON, it) }
                lat?.let { KvUtils.encode(Constant.END_LAT, it) }
            }
        }
    }

    private fun setEndLonLatData(lon: String?, lat: String?) {
        //经度
        val degreesToSecondsLon = Util.degreesToSeconds("" + lon)
        val splitLon = degreesToSecondsLon.split("|")

        if (splitLon.size == 3) {
            mDatabind.endLonDu.setText(splitLon[0])
            mDatabind.endLonFen.setText(splitLon[1])
            mDatabind.endLonMiao.setText(splitLon[2])
        }

        //纬度
        val degreesToSecondsLat = Util.degreesToSeconds("" + lat)
        val splitLat = degreesToSecondsLat.split("|")

        if (splitLat.size == 3) {
            mDatabind.endLatDu.setText(splitLat[0])
            mDatabind.endLatFen.setText(splitLat[1])
            mDatabind.endLatMiao.setText(splitLat[2])
        }
    }

    private fun setStartLonLatData(lon: String?, lat: String?) {
        //经度
        val degreesToSecondsLon = Util.degreesToSeconds("" + lon)
        val splitLon = degreesToSecondsLon.split("|")

        if (splitLon.size == 3) {
            mDatabind.startLonDu.setText(splitLon[0])
            mDatabind.startLonFen.setText(splitLon[1])
            mDatabind.startLonMiao.setText(splitLon[2])
        }

        //纬度
        val degreesToSecondsLat = Util.degreesToSeconds("" + lat)
        val splitLat = degreesToSecondsLat.split("|")

        if (splitLat.size == 3) {
            mDatabind.startLatDu.setText(splitLat[0])
            mDatabind.startLatFen.setText(splitLat[1])
            mDatabind.startLatMiao.setText(splitLat[2])
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.startMapLocation,
            mDatabind.endMapLocation,
            mDatabind.startMapLocationClear,
            mDatabind.endMapLocationClear,
            mDatabind.mineToolsMapIndexCommit
        ) {
            when (it) {
                mDatabind.startMapLocationClear -> {
                    mDatabind.startLonDu.setText("")
                    mDatabind.startLonFen.setText("")
                    mDatabind.startLonMiao.setText("")
                    mDatabind.startLatDu.setText("")
                    mDatabind.startLatFen.setText("")
                    mDatabind.startLatMiao.setText("")
                    KvUtils.encode(Constant.START_LON, "")
                    KvUtils.encode(Constant.START_LAT, "")
                }

                mDatabind.endMapLocationClear -> {
                    mDatabind.endLonDu.setText("")
                    mDatabind.endLonFen.setText("")
                    mDatabind.endLonMiao.setText("")
                    mDatabind.endLatDu.setText("")
                    mDatabind.endLatFen.setText("")
                    mDatabind.endLatMiao.setText("")
                    KvUtils.encode(Constant.END_LON, "")
                    KvUtils.encode(Constant.END_LAT, "")
                }

                mDatabind.startMapLocation -> {
                    permissionRequest("start")
                }

                mDatabind.endMapLocation -> {
                    permissionRequest("end")
                }

                mDatabind.mineToolsMapIndexCommit -> {
                    checkedCommit()
                }
            }
        }
    }

    private fun permissionRequest(pointType: String) {
        XXPermissions.with(this)
            .permission(Permission.ACCESS_FINE_LOCATION)
            .interceptor(PermissionInterceptor())
            .request { _, allGranted ->
                if (allGranted) {
                    when (pointType) {
                        "start" -> {
                            selectStartLocation()
                        }

                        "end" -> {
                            selectEndLocation()
                        }
                    }
                } else {
                    nav().navigateUp()
                }
            }
    }

    private fun checkedCommit() {
        //放飞 经
        val slonDu = mDatabind.startLonDu.text.toString()
        val slonFen = mDatabind.startLonFen.text.toString()
        val slonMiao = mDatabind.startLonMiao.text.toString()
        //放飞 纬
        val slatDu = mDatabind.startLatDu.text.toString()
        val slatFen = mDatabind.startLatFen.text.toString()
        val slatMiao = mDatabind.startLatMiao.text.toString()
        //归巢 经
        val elonDu = mDatabind.endLonDu.text.toString()
        val elonFen = mDatabind.endLonFen.text.toString()
        val elonMiao = mDatabind.endLonMiao.text.toString()
        //归巢 纬
        val elatDu = mDatabind.endLatDu.text.toString()
        val elatFen = mDatabind.endLatFen.text.toString()
        val elatMiao = mDatabind.endLatMiao.text.toString()

        if (checkLatLonNull(slonDu, slonFen, slonMiao, slatDu, slatFen, slatMiao, elonDu, elonFen, elonMiao, elatDu, elatFen, elatMiao)) {
            return
        }

        if (isBOSMapEmpty(
                hashMapOf(
                    slatDu to "放飞地纬度错误",
                    slatFen to "放飞地纬度错误",
                    slatMiao to "放飞地纬度错误",

                    slonDu to "放飞地经度错误",
                    slonFen to "放飞地经度错误",
                    slonMiao to "放飞地经度错误",

                    elatDu to "归巢地纬度错误",
                    elatFen to "归巢地纬度错误",
                    elatMiao to "归巢地纬度错误",

                    elonDu to "归巢地经度错误",
                    elonFen to "归巢地经度错误",
                    elonMiao to "归巢地经度错误"
                )
            )
        ) {
            return
        }

        val slon = Util.secondsToDegrees(slonDu, slonFen, slonMiao)
        val slat = Util.secondsToDegrees(slatDu, slatFen, slatMiao)
        val elon = Util.secondsToDegrees(elonDu, elonFen, elonMiao)
        val elat = Util.secondsToDegrees(elatDu, elatFen, elatMiao)
        if ((slon.equals(elon)) && (slat.equals(elat))) {
            RxToast.showToast("放飞地和归巢地不能相同,请重新选择")
            return
        }
        if (!Util.latLonisInChinaTaiWanDiaoYu(slon.toDouble(), slat.toDouble())) {
            RxToast.showToast("放飞地" + getString(R.string.lat_lon_not_in_china))
            return
        }
        if (!Util.latLonisInChinaTaiWanDiaoYu(elon.toDouble(), elat.toDouble())) {
            RxToast.showToast("归巢地" + getString(R.string.lat_lon_not_in_china))
            return
        }
        slon?.let { KvUtils.encode(Constant.START_LON, it) }
        slat?.let { KvUtils.encode(Constant.START_LAT, it) }
        elon?.let { KvUtils.encode(Constant.END_LON, it) }
        elat?.let { KvUtils.encode(Constant.END_LAT, it) }
        showLoading(getString(R.string.loading))
        mViewModel.getWeatherLine(slon, slat, elon, elat)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.flyWeatherResult.observe(viewLifecycleOwner) {
            dismissLoading()
            if (it != null) {
                nav().navigateAction(R.id.action_to_match_line_detail, Bundle().apply {
                    putParcelable("data", it)
                })
            }
        }
    }

    private fun selectEndLocation() {
        //归巢 经
        val elonDu = mDatabind.endLonDu.text.toString()
        var elonFen = mDatabind.endLonFen.text.toString()
        var elonMiao = mDatabind.endLonMiao.text.toString()
        //归巢 纬
        val elatDu = mDatabind.endLatDu.text.toString()
        var elatFen = mDatabind.endLatFen.text.toString()
        var elatMiao = mDatabind.endLatMiao.text.toString()

        if (TextUtils.isEmpty(elonDu) || TextUtils.isEmpty(elatDu)) {
            nav().navigateAction(R.id.action_to_select_map_point, Bundle().apply {
                putBoolean("islocation", true)
                putBoolean("is_start", false)
            })
        } else {
            if (TextUtils.isEmpty(elonFen)) elonFen = "0"
            if (TextUtils.isEmpty(elonMiao)) elonMiao = "0"

            if (TextUtils.isEmpty(elatFen)) elatFen = "0"
            if (TextUtils.isEmpty(elatMiao)) elatMiao = "0"

            val elon = Util.secondsToDegrees(elonDu, elonFen, elonMiao)
            val elat = Util.secondsToDegrees(elatDu, elatFen, elatMiao)

            if (!Util.latLonisInChinaTaiWanDiaoYu(elon.toDouble(), elat.toDouble())) {
                RxToast.showToast("归巢地" + getString(R.string.lat_lon_not_in_china))
                return
            }
            if (checkLatLonNullEnd(elonDu, elonFen, elonMiao, elatDu, elatFen, elatMiao)) {
                return
            }
            nav().navigateAction(R.id.action_to_select_map_point, Bundle().apply {
                putBoolean("islocation", false)
                putBoolean("is_start", false)
                putDouble("lon", elon.toDouble())
                putDouble("lat", elat.toDouble())
            })

        }
    }

    private fun selectStartLocation() {
        //放飞 经
        val slonDu = mDatabind.startLonDu.text.toString()
        var slonFen = mDatabind.startLonFen.text.toString()
        var slonMiao = mDatabind.startLonMiao.text.toString()
        //放飞 纬
        val slatDu = mDatabind.startLatDu.text.toString()
        var slatFen = mDatabind.startLatFen.text.toString()
        var slatMiao = mDatabind.startLatMiao.text.toString()

        if (TextUtils.isEmpty(slonDu) || TextUtils.isEmpty(slatDu)) {
            nav().navigateAction(R.id.action_to_select_map_point, Bundle().apply {
                putBoolean("islocation", true)
                putBoolean("is_start", true)
            })
        } else {
            if (TextUtils.isEmpty(slonFen)) slonFen = "0"
            if (TextUtils.isEmpty(slonMiao)) slonMiao = "0"

            if (TextUtils.isEmpty(slatFen)) slatFen = "0"
            if (TextUtils.isEmpty(slatMiao)) slatMiao = "0"

            if (checkLatLonNullStart(slonDu, slonFen, slonMiao, slatDu, slatFen, slatMiao)) {
                return
            }
            val slon = Util.secondsToDegrees(slonDu, slonFen, slonMiao)
            val slat = Util.secondsToDegrees(slatDu, slatFen, slatMiao)

            if (!Util.latLonisInChinaTaiWanDiaoYu(slon.toDouble(), slat.toDouble())) {
                RxToast.showToast("放飞地" + getString(R.string.lat_lon_not_in_china))
                return
            }

            nav().navigateAction(R.id.action_to_select_map_point, Bundle().apply {
                putBoolean("islocation", false)
                putBoolean("is_start", true)
                putDouble("lon", slon.toDouble())
                putDouble("lat", slat.toDouble())
            })
        }
    }

    private fun checkLatLonNullStart(slonDu: String, slonFen: String, slonMiao: String, slatDu: String, slatFen: String, slatMiao: String): Boolean {
        var slonDu1 = slonDu
        var slonFen1 = slonFen
        var slonMiao1 = slonMiao
        var slatDu1 = slatDu
        var slatFen1 = slatFen
        var slatMiao1 = slatMiao
        if (TextUtils.isEmpty(slonDu)) slonDu1 = "0"
        if (TextUtils.isEmpty(slonFen)) slonFen1 = "0"
        if (TextUtils.isEmpty(slonMiao)) slonMiao1 = "0"
        if (TextUtils.isEmpty(slatDu)) slatDu1 = "0"
        if (TextUtils.isEmpty(slatFen)) slatFen1 = "0"
        if (TextUtils.isEmpty(slatMiao)) slatMiao1 = "0"

        if (slatDu1.toInt() > 90) {
            RxToast.showToast("放飞地纬度,度不能大于90")
            return true
        }
        if (slatFen1.toInt() >= 60) {
            RxToast.showToast("放飞地纬度,分不能大于60")
            return true
        }
        if (slatMiao1.toDouble() >= 60.0) {
            RxToast.showToast("放飞地纬度,秒不能大于60")
            return true
        }

        if (slonDu1.toInt() > 180) {
            RxToast.showToast("放飞地经度,度不能大于180")
            return true
        }
        if (slonFen1.toInt() >= 60) {
            RxToast.showToast("放飞地经度,分不能大于60")
            return true
        }
        if (slonMiao1.toDouble() >= 60.0) {
            RxToast.showToast("放飞地经度,秒不能大于60")
            return true
        }
        return false
    }

    private fun checkLatLonNullEnd(elonDu: String, elonFen: String, elonMiao: String, elatDu: String, elatFen: String, elatMiao: String): Boolean {
        var elonDu1 = elonDu
        var elonFen1 = elonFen
        var elonMiao1 = elonMiao
        var elatDu1 = elatDu
        var elatFen1 = elatFen
        var elatMiao1 = elatMiao
        if (TextUtils.isEmpty(elonDu)) elonDu1 = "0"
        if (TextUtils.isEmpty(elonFen)) elonFen1 = "0"
        if (TextUtils.isEmpty(elonMiao)) elonMiao1 = "0"
        if (TextUtils.isEmpty(elatDu)) elatDu1 = "0"
        if (TextUtils.isEmpty(elatFen)) elatFen1 = "0"
        if (TextUtils.isEmpty(elatMiao)) elatMiao1 = "0"
        if (elatDu1.toInt() > 90) {
            RxToast.showToast("归巢地纬度,度不能大于90")
            return true
        }
        if (elatFen1.toInt() >= 60) {
            RxToast.showToast("归巢地纬度,分不能大于60")
            return true
        }
        if (elatMiao1.toDouble() >= 60.0) {
            RxToast.showToast("归巢地纬度,秒不能大于60")
            return true
        }

        if (elonDu1.toInt() > 180) {
            RxToast.showToast("归巢地经度,度不能大于180")
            return true
        }
        if (elonFen1.toInt() >= 60) {
            RxToast.showToast("归巢地经度,分不能大于60")
            return true
        }
        if (elonMiao1.toDouble() >= 60.0) {
            RxToast.showToast("归巢地经度,秒不能大于60")
            return true
        }
        return false
    }

    private fun checkLatLonNull(
        slonDu: String,
        slonFen: String,
        slonMiao: String,
        slatDu: String,
        slatFen: String,
        slatMiao: String,
        elonDu: String,
        elonFen: String,
        elonMiao: String,
        elatDu: String,
        elatFen: String,
        elatMiao: String
    ): Boolean {
        var slonDu1 = slonDu
        var slonFen1 = slonFen
        var slonMiao1 = slonMiao
        var slatDu1 = slatDu
        var slatFen1 = slatFen
        var slatMiao1 = slatMiao
        var elonDu1 = elonDu
        var elonFen1 = elonFen
        var elonMiao1 = elonMiao
        var elatDu1 = elatDu
        var elatFen1 = elatFen
        var elatMiao1 = elatMiao
        if (TextUtils.isEmpty(slonDu)) slonDu1 = "0"
        if (TextUtils.isEmpty(slonFen)) slonFen1 = "0"
        if (TextUtils.isEmpty(slonMiao)) slonMiao1 = "0"
        if (TextUtils.isEmpty(slatDu)) slatDu1 = "0"
        if (TextUtils.isEmpty(slatFen)) slatFen1 = "0"
        if (TextUtils.isEmpty(slatMiao)) slatMiao1 = "0"
        if (TextUtils.isEmpty(elonDu)) elonDu1 = "0"
        if (TextUtils.isEmpty(elonFen)) elonFen1 = "0"
        if (TextUtils.isEmpty(elonMiao)) elonMiao1 = "0"
        if (TextUtils.isEmpty(elatDu)) elatDu1 = "0"
        if (TextUtils.isEmpty(elatFen)) elatFen1 = "0"
        if (TextUtils.isEmpty(elatMiao)) elatMiao1 = "0"
        if (slatDu1.toInt() > 90) {
            RxToast.showToast("放飞地纬度,度不能大于90")
            return true
        }
        if (slatFen1.toInt() >= 60) {
            RxToast.showToast("放飞地纬度,分不能大于60")
            return true
        }
        if (slatMiao1.toDouble() >= 60.0) {
            RxToast.showToast("放飞地纬度,秒不能大于60")
            return true
        }

        if (slonDu1.toInt() > 180) {
            RxToast.showToast("放飞地经度,度不能大于180")
            return true
        }
        if (slonFen1.toInt() >= 60) {
            RxToast.showToast("放飞地经度,分不能大于60")
            return true
        }
        if (slonMiao1.toDouble() >= 60.0) {
            RxToast.showToast("放飞地经度,秒不能大于60")
            return true
        }

        if (elatDu1.toInt() > 90) {
            RxToast.showToast("归巢地纬度,度不能大于90")
            return true
        }
        if (elatFen1.toInt() >= 60) {
            RxToast.showToast("归巢地纬度,分不能大于60")
            return true
        }
        if (elatMiao1.toDouble() >= 60.0) {
            RxToast.showToast("归巢地纬度,秒不能大于60")
            return true
        }

        if (elonDu1.toInt() > 180) {
            RxToast.showToast("归巢地经度,度不能大于180")
            return true
        }
        if (elonFen1.toInt() >= 60) {
            RxToast.showToast("归巢地经度,分不能大于60")
            return true
        }
        if (elonMiao1.toDouble() >= 60.0) {
            RxToast.showToast("归巢地经度,秒不能大于60")
            return true
        }
        return false
    }
}